package cn.tedu._05mvcboot06.exception;

import cn.tedu._05mvcboot06.common.response.JsonResult;
import cn.tedu._05mvcboot06.common.response.StatusCode;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult MethodArgumentNotValidException(MethodArgumentNotValidException ex){
  String message =  ex.getFieldError().getDefaultMessage();
   return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }


    @ExceptionHandler
    public JsonResult ConstraintViolationException(ConstraintViolationException ex){
        String message = ex.getMessage().split(":")[1].trim();
        return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }
}
